package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtils;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
@Tag(name = "员工相关接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    @Operation(summary = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){//前端传用户名和密码过来
        log.info("员工登录：{}",employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);//查出来返回出员工对象
        Map<String, Object> claims=new HashMap<>();//查得到就开始构建token
        claims.put("empId",employee.getId());
        String token = JwtUtils.createJwt(claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()//用builder构建VO返回前端
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        log.info("员工登录成功：{}",employeeLoginVO);
        return Result.success(employeeLoginVO);
    }
    @PostMapping
    @Operation(summary = "员工注册")
    public Result save(@RequestBody EmployeeDTO employeeDTO){//传过来得DTO包括用户名、姓名、电话、性别、身份证号
        log.info("员工注册：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @Operation(summary = "员工分页查询")
    public Result<PageResult> page(@ParameterObject EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.page(employeePageQueryDTO);//用MP得，这个不看了
        return Result.success(pageResult);
    }
    @PostMapping("/status/{status}")
    @Operation(summary = "员工状态修改")
    public Result startOrStop(@PathVariable("status") Integer status, Long id){//路径参数+请求参数，post请求
        log.info("启用禁用员工状态:{},{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据员工id查询详情")
    public Result<Employee> getById(@PathVariable("id") Long id){//直接使用MP实现
        Employee employee=employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @Operation(summary = "员工信息修改")
    public Result update(@RequestBody EmployeeDTO employeeDTO){//可以用MP根据id修改
        log.info("员工信息修改：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
